package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.SystemMessage;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.SystemMessageMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService{
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private SystemMessageMapper systemMessageMapper;

    @Override
    public void save(Travel travel) {
        //设置默认数据
        //创建时间,当前时间
        travel.setCreateTime(new Date());

        //最后更新时间,当前时间
        travel.setLastUpdateTime(new Date());

        //作者: 当前登录用户(在controller设置好了)
        //设置状态: 如果与发布共用一个方法,此处不能这么干
        travel.setState(Travel.STATE_NORMAL);

        //添加操作
        //必须返回自动增长的id
        travelMapper.insert(travel);//游记基本信息

        TravelContent travelContent = travel.getTravelContent();
        travelContent.setId(travel.getId());

        //保存游记内容
        travelContentMapper.insert(travelContent);

    }

    @Override
    public void update(Travel travel) {
        //修改最近修改时间
        travel.setLastUpdateTime(new Date());

        //基本信息
        travelMapper.updateByPrimaryKey(travel);

        //游记内容更新
        TravelContent travelContent = travel.getTravelContent();
        travelContent.setId(travel.getId());
        travelContentMapper.updateByPrimaryKey(travelContent);
    }

    @Override
    public Travel get(Long id) {
        TravelContent travelContent = travelContentMapper.selectByPrimaryKey(id);

        return travelMapper.selectByPrimaryKey(id);
    }


    @Override
    public PageInfo selectForList(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void changeState(Travel travel, Long authorId) {
        //若是发布,则增加发布时间
        if(travel.getState() == Travel.STATE_RELEASE){
            travel.setReleaseTime(new Date());
        }
        int result = travelMapper.changeState(travel);

        //修改状态后后台给客户发送系统通知
        if (result != 0 ) {
            User user = new User();
            user.setId(authorId);
            SystemMessage systemMessage = new SystemMessage();
            systemMessage.setUser(user);
            systemMessage.setSystemtime(new Date());
            systemMessage.setTravel(travel);
            if(travel.getState() == 0 ){
                systemMessage.setContent("游记已被拒绝");
                systemMessageMapper.insert(systemMessage);
            }else if(travel.getState() == 3){
                systemMessage.setContent("游记审核通过");
                systemMessageMapper.insert(systemMessage);
            }else if(travel.getState() == 2){
                systemMessage.setContent("游记需要审核");
                systemMessageMapper.insert(systemMessage);
            }
        }

    }
    @Override
    public Travel selectByTravelId(Long travelId) {
        return travelMapper.selectByPrimaryKey(travelId);
    }

    @Override
    public void deleteRealtion(Long travelId, Long strategyId) {
        travelMapper.deleteRealtion(travelId, strategyId);
    }

    @Override
    public List<Travel> getRegionTravelByStrategyId(Long strategyId) {
        return travelMapper.getRegionTravelByStrategyId(strategyId);
    }

    @Override
    public void insertRelation(Long strategyId, Long travelId) {
        travelMapper.insertRelation(strategyId, travelId);
    }

    @Override
    public List<Travel> getTravelByStrategyId(Long strategyId) {
        return travelMapper.selectTravelByStrategyId(strategyId);
    }

    @Override
    public  List<Travel> selectByTargetId(Long targetId) {
        return travelMapper.selectByTargetId(targetId);
    }


}
